package com.cuitrip.business.search;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;

import com.cuitrip.apiservice.SearchBusiness;
import com.cuitrip.apiservice.TrackBusiness;
import com.cuitrip.business.home.travel.TravelTripListActivity;
import com.cuitrip.business.search.model.HotWord;
import com.cuitrip.business.search.model.HotWordListResponse;
import com.cuitrip.business.track.TrackProxy;
import com.cuitrip.service.R;
import com.cuitrip.util.FlowLayout;
import com.lab.adapter.HolderViewAdapter;
import com.lab.app.BaseActivity;
import com.lab.jumper.Jumper;
import com.lab.network.config.CtResponseCode;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.utils.LogHelper;
import com.lab.utils.MessageUtils;
import com.lab.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 搜索页面 activity
 * Created  ak
 */
public class SearchActivity extends BaseActivity implements IProxyCallback {

    public static final String TAG = "SearchActivity";

    @Bind(R.id.search_box)
    protected EditText searchBox;

   /* @Bind(R.id.hot_words_grid)
    protected GridView hotWordsGridView;*/
   @Bind(R.id.hot_words_grid)
   protected ScrollView hotWordsGridView;

    private HolderViewAdapter mAdapter;

    private List<HotWord> hotWordList = new ArrayList<>();

    private String mQuery;
    private ApiProxy mApiProxy;
    private FlowLayout mFlowLayout;


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_search_view);
        ButterKnife.bind(this);
        TrackProxy.getInstance().reviewPage(TrackBusiness.Source.HOT_SEARCH, "");
        mApiProxy=new ApiProxy(this);
        mFlowLayout = new FlowLayout(this);
        requestHotWords();
        searchBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((event == null || event.getAction() == KeyEvent.ACTION_UP)
                        && (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_GO || actionId == EditorInfo.IME_ACTION_UNSPECIFIED || actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT)) {
                    TrackProxy.getInstance().searchWords(TrackBusiness.Source.HOT_SEARCH, searchBox.getText().toString(), "");
                    requestSearch(searchBox.getText());
                }
                return true;
            }
        });
    }

    private void requestSearch(CharSequence charSequence){
        String q = charSequence.toString().trim();
        mQuery = q;
        if (TextUtils.isEmpty(q)) {
            return;
        }
        Intent intent = new Intent(this, TravelTripListActivity.class);
        intent.putExtra(TravelTripListActivity.KEY_WORD_KEY, mQuery);
        intent.putExtra(TravelTripListActivity.TITLE_KEY, mQuery);
        intent.putExtra(TAG,TAG);
        Jumper.launchActivity(this, intent);
    }

    private void requestHotWords(){
        SearchBusiness.getHotWordList(mApiProxy);
    }
    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        CtApiResponse response = (CtApiResponse) result.getData();
        if (response!= null && response.code == CtResponseCode.RESULT_OK){
            if (response.result instanceof HotWordListResponse) {
                LogHelper.w(TAG, response.toString());
                HotWordListResponse<HotWord> hotWordListResponse = (HotWordListResponse<HotWord>) response.result;
                for(int i=0;i<hotWordListResponse.getList().size();++i){
                    final TextView textView=new TextView(getBaseContext());
                    final String hotWord=hotWordListResponse.getList().get(i);
                    textView.setText(hotWord);
                    textView.setGravity(Gravity.CENTER);
                    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
                    int padding1= Utils.dp2pixel(16);
                    int padding2=Utils.dp2pixel(8);
                    textView.setPadding(padding1, padding2, padding1, padding2);
                    textView.setBackgroundResource(R.drawable.select_alpha_orange_corner);
                    textView.setTextColor(getResources().getColorStateList(R.color.ct_white_qiaomai));
                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            TrackProxy.getInstance().searchWords(TrackBusiness.Source.HOT_SEARCH,"",textView.getText().toString());
                            requestSearch(textView.getText());
                        }
                    });
                    mFlowLayout.addView(textView);
                }
                hotWordsGridView.addView(mFlowLayout);
                //inflateList(hotWordListResponse.getList());
            }
        }else{
            if (response != null && !TextUtils.isEmpty(response.msg)) {
                MessageUtils.showToast(response.msg);
            }
        }
        return false;
    }


    private void inflateList(List<String> tempList) {
        hotWordList.clear();
        List<HotWord> tempHotWordList = new ArrayList<>();
        for (String text : tempList){
            HotWord hotWord = new HotWord(text);
            tempHotWordList.add(hotWord);
        }
        hotWordList.addAll(tempHotWordList);
       // mAdapter.notifyDataSetChanged();
    }


    @OnClick(R.id.cancel_action)
    public void cancelSearch(){
        finish();
    }


}
